//
//  RetweetStatusCell.swift
//  DSWeibo
//
//  Created by J on 16/4/30.
//  Copyright © 2016年 J. All rights reserved.
//

import UIKit

class RetweetStatusCell: StatusTableViewCell {

    let screenWidth = UIScreen.mainScreen().bounds.size.width

    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        
        
    }
    

    
    //重写父类的didSet并不会覆盖父类的操作
    //只需要在重写方法中做自己想做的事即可
    //注意点:如果父类事didSet, 那么子类也只能重写didSet
    override var status: Statuses?{
        didSet{
            let retweetStatus = status?.retweeted_status
          
            let name = retweetStatus?.user?.name ?? ""
            let text = retweetStatus?.text ?? ""
            retweetContentLabel.text = name + ": " + text
        }
    }

    override func setupUI(){
        super.setupUI()
        //1.添加子控件
        contentView.insertSubview(backGroundButton, belowSubview: pictureView)
        contentView.insertSubview(retweetContentLabel, aboveSubview: backGroundButton)

        retweetContentLabel.text = ""
        
        let superView = contentView
        let padding = 10
        
        //2.1布局转发正文
        
        retweetContentLabel.snp_makeConstraints { (make) in
            make.left.equalTo(superView).offset(padding)
            make.top.equalTo(contentLabel.snp_bottom).offset(padding)
            make.bottom.equalTo(pictureView.snp_top).offset(-pictureViewTopPadding)
        }

        
        //配图
        pictureView.snp_remakeConstraints { (make) in
            make.left.equalTo(contentLabel)
        }
        


        footerView.snp_updateConstraints { (make) in
            make.top.equalTo(pictureView.snp_bottom).offset(padding)
        }
        
        
        backGroundButton.snp_makeConstraints { (make) in
            make.left.equalTo(footerView)
            make.right.equalTo(footerView)
            make.bottom.equalTo(footerView.snp_top).offset(-padding)
            make.top.equalTo(retweetContentLabel)
        }

        
     
        footerView.backgroundColor = UIColor.orangeColor()

        
    }
    
    //获取行高
   override func rowHeight(status:Statuses) -> (CGFloat){
        //1.
        self.status = status.retweeted_status//调用didSet计算配图高度
        //2. 更新整个cell
        self.layoutIfNeeded()
        //3.返回底部视图最大Y
        print(footerView , backGroundButton)
        return CGRectGetMaxY(footerView.frame) + 44//TODO: 高度未修复
    }
    
    
    



    ///正文
     lazy var retweetContentLabel : UILabel = {
        let label = UILabel.createLabel(UIColor.lightGrayColor(), font:15 )
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 20
        return label
    }()

    
    //点击跳转按钮
    private lazy var backGroundButton: UIButton = {
       let btn = UIButton()
        btn.backgroundColor = UIColor(white: 0.9, alpha: 0.5)
        btn.addTarget(self, action: #selector(RetweetStatusCell.click), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
    }()
    
    

    func click(button:UIButton){
        print(#function)
        
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}
